<?php
namespace app\app\controller\v1;
use think\Db;
use think\Request;
use app\app\util\OnlineUtils;

require_once(dirname(dirname(dirname(__FILE__))).'/util/phpqrcode/phpqrcode.php');
use phpqrcode\QRcode;

class UserController extends BaseController
{
   /**
     * 显示资源列表
     * 
     * @return \think\Response
     */
    public function index(Request $request)
    {	$header = $request->header();
        $user_id = OnlineUtils::getUserIdBySid();
    	$user = Db::name('users')->field('id,portrait,username,wx_nick_name,sex,wx_number')->where('id','eq',$user_id)->where('status','eq',1)->find();	
    	$user['token'] = $header['token'];
		return hmp(200,'success',['data'=>$user]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {

    }

    /**
     * 保存新建的资源
     * 点赞
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {
    	return hmp(200,'success');
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
    	$id = intval(input('id'));
		$user_id = OnlineUtils::getUserIdBySid();
    	$user = Db::name('users')->field('id,wx_number,username,address,phone,description,portrait,wx_nick_name,sex')->where('id','eq',$user_id)->where('status','eq',1)->find();
    	return hmp(200,'success',['data'=>$user]);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update($id)
    {

    }

    /**
     * 保存更新的资源
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function updateUserInfo(Request $request)
    {
        $user_id = OnlineUtils::getUserIdBySid();
        $data = $request->param();
        $user['username'] = $data['username'];
        $user['sex'] = $data['sex'];
        $user['phone'] = $data['phone'];
        $user['address'] = $data['address'];
        $user['description'] = $data['description'];
        Db::name('users')->where('id','eq',$user_id)->update($user);
		$user_data = Db::name('users')->field('id,wx_number,username,address,phone,description,portrait,wx_nick_name,sex')->where('id','eq',$user_id)->find();
        return hmp(200,'success',['data'=>$user_data]);        
    }	
	
    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {

    }
	
    /**
     * [invite 我的邀请/生成二维码]
     * @return [type] [description]
     */
    public function invite()
    {
        $user_id = OnlineUtils::getUserIdBySid();
        $user = Db::name('users')->field('id,portrait,username,wx_nick_name,wx_number,sex,invite_code,address')->where('id','eq',$user_id)->where('status','eq',1)->find();
        $value = json_encode($user);      //二维码内容  
        $errorCorrectionLevel = 'L';    //容错级别   
        $matrixPointSize = 5;           //生成图片大小    
        //生成二维码图片
        $name = microtime(); 
        $filename = ROOT_PATH.'public'.DS.'/phpqrcode/'.$name.'.png';  
        $QR = QRcode::png($value,$filename, $errorCorrectionLevel, $matrixPointSize, 2);
        return hmp(200,'success',['data'=>config('QRcode').$name.'.png']);
    }
	
    public function getAddress()
    {
        $user_id  = OnlineUtils::getUserIdBySid();
        $lat      = input('post.lat') ? input('post.lat') : '';
        $lng      = input('post.lng') ? input('post.lng') : '';
        $location = $lat.','.$lng;
        $data['location'] = $location;
        $data['key'] = config('BAIDU_KEY');
        $url      = "http://apis.map.qq.com/ws/geocoder/v1/";
        $result   = makeRequest($url,$data);
        return hmp(200,'success',$result);
    }	
	
}